package com.cdream.basic.algorithm.leetcode.findElement;

import java.util.HashMap;
import java.util.Map;

/**
 * @author cdream
 * @date 5/22/21
 */
public class Question290 {
    public boolean wordPattern(String pattern, String s) {
        Map<Character, String> map = new HashMap<>();

        String[] s1 = s.split(" ");
        if (pattern.length() != s1.length) {
            return false;
        }
        for (int i = 0; i < pattern.length(); i++) {
            String c = map.get(pattern.charAt(i));
            if (c == null) {
                if (map.containsValue(s1[i])) {
                    return false;
                }
                map.put(pattern.charAt(i), s1[i]);
            } else {
                if (!c.equals(s1[i])) {
                    return false;
                }
            }
        }
        return true;

    }
}
